package io.gitee.chearnee.fw.dingtalk.manager;

import io.gitee.chearnee.fw.dingtalk.enums.AppType;
import io.gitee.chearnee.fw.dingtalk.robot.DingTalkRobot;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2021/7/27 19:55
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DingTalkManager {

  private final List<DingTalk> dingTalkList;
  private ConcurrentHashMap<AppType, DingTalk> talkConcurrentHashMap = null;

  @PostConstruct
  public void initDingTalk() {
    if (talkConcurrentHashMap == null) {
      talkConcurrentHashMap = new ConcurrentHashMap<>(dingTalkList.size());
    }
    dingTalkList.forEach(dingTalk -> talkConcurrentHashMap.put(dingTalk.appType, dingTalk));
  }

  @PreDestroy
  public void removeDingTalk() {
    talkConcurrentHashMap.clear();
  }
  public DingTalkRobot getRobot(String name) {
    DingTalkRobot dingTalk = (DingTalkRobot) talkConcurrentHashMap.get(AppType.ROBOT);
    dingTalk.setDingTalkName(name);
    return dingTalk;
  }

  /**
   * 重置钉钉某个模块
   *
   * @param
   */
  public void reloadDingTalk() {
    initDingTalk();
  }
}
